package com.zengde.ai_sandbox.service.strategy.impl;

import com.zengde.ai_sandbox.context.OrderContext;
import com.zengde.ai_sandbox.context.OrderContextHolder;
import com.zengde.ai_sandbox.entity.Order;
import com.zengde.ai_sandbox.service.strategy.PaymentStrategy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("abstractPaymentStrategy")
public abstract class AbstractPaymentStrategy implements PaymentStrategy {

    // 1. 检查订单状态是否合法
    private void checkOrderStatus() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        // 检查订单是否存在且支付金额大于0
        if (order == null || order.getPaymentAmount() == null || order.getPaymentAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalStateException("订单状态不合法: 订单为空或支付金额不正确");
        }
        
        System.out.println("订单状态检查通过: 订单号=" + order.getOrderNo() + ", 支付金额=" + order.getPaymentAmount());
    }
    
    // 2. 调用远程接口
    public abstract void invokeRmtInterface();

    // 微信支付特殊逻辑
    public abstract void executePaymentSpecialLogic();
    
    // 3. 更新订单状态
    private void updateOrderStatus() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        System.out.println("开始更新订单状态: 订单号=" + order.getOrderNo());
        
        // 模拟更新订单状态
        order.setPaymentType("WECHAT");
        
        System.out.println("订单状态更新完成: 订单号=" + order.getOrderNo()
                + ", 支付方式 "+ order.getPaymentType()
                +", 实际支付金额=" + orderContext.getActualPaymentAmount());
    }
    
    @Override
    public void process() {
        // 1. 通用：检查订单状态是否合法
        checkOrderStatus();
        
        // 2. 调用各自的远程接口
        invokeRmtInterface();

        // 2.1 执行微信支付特殊的逻辑
        executePaymentSpecialLogic();
        
        // 3. 通新 ：更新订单状态
        updateOrderStatus();

    }
}